Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTG] Add ElaborationPass #7876

Merged
merged 1 commit into from
Dec 9, 2024
Merged

[RTG] Add ElaborationPass #7876

merged 1 commit into from
Dec 9, 2024

Conversation

maerhart
Copy link
Member

Add a pass that lowers the random parts of RTG IR.

@maerhart maerhart added the RTG Involving the `rtg` dialect label Nov 22, 2024
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch from 36dfd4c to 2633cab Compare November 22, 2024 17:43
Copy link
Member

@youngar youngar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will keep looking tomorrow, its looking great! 😁

docs/Dialects/RTG.md Outdated Show resolved Hide resolved
include/circt/Dialect/RTG/Transforms/RTGPasses.h Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
Comment on lines 503 to 587
// If the test requries nothing from a target, we can always run it.
if (test.getTarget().getEntryTypes().empty())
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if a test does not have any requirements we won't run it on each target, just once in general?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two things to consider:

  1. The unelaborated test is the same for all targets, so do we want to randomize it once for each target or randomize it once and use that for each target? If the latter, then we don't have to copy it. But I guess the earlier approach would be the more consistent one, right?
  2. Currently I assume that all tests for all targets will be run on the same system because there is no way to distinguish them after this pass. If we want to have targets for different systems in the same MLIR file, we'd need some scoping, add the target that the test specialized for as an attribute, or some other way to distinguish them. As long as we don't do that, we don't need to duplicate a test that has the same content.

I guess I will change it to be copied and independently randomized for each target, but leave point 2 for a future PR.

Copy link
Member

@youngar youngar Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know whats best, what you had originally is probably good! We can revisit these points in the future easily enough.

test/Dialect/RTG/Transform/elaboration.mlir Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
@maerhart maerhart force-pushed the maerhart-rtg-test-and-target branch 3 times, most recently from 506937f to 14a434c Compare November 26, 2024 13:04
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch from 2633cab to b89ad7c Compare November 26, 2024 14:06
Base automatically changed from maerhart-rtg-test-and-target to main December 2, 2024 09:47
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch from b89ad7c to df7c685 Compare December 2, 2024 09:52
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch from df7c685 to f8cb26a Compare December 2, 2024 18:33
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch 3 times, most recently from 07d1ea4 to 07d7892 Compare December 3, 2024 16:10
Copy link
Member Author

@maerhart maerhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I have removed handling of sequences to simplify this PR and will create another PR with those features. Support for sequences is far from trivial and having them in another PR allows us to focus on sets in this PR.
  • I've removed opaque/unevaluated values for now and will also add them in a future PR such that we can focus on the issues that this can introduce in a dedicated PR
  • I've added an AttributeValue to be able to add tests now that we don't support sequences anymore (this is easier to reason about and we need it anyway)
  • I've fixed the dominance issues by introducing a materializer that properly caches materialized values per block and by removing opaque value support for now
  • I've fixed an issue where I iterated over a non-deterministic set order. Basically we need to keep track of some deterministic order for sets in general not just in debug mode. Thus I replaced it with a SetVector.
  • Removed the rtg.elaboration attribute support in favour of rtg.elaboration_custom_seed with specifies a seed just for that particular op and removed debug mode. Being able to specify the index in the set is more complicated to handle for not a lot of benefit. The purpose for the attribute is to make it easier to write tests (adding a test at some point should not influence the random selection at another place).
  • I've added some additional debug printing support for which I had a separate PR, but I think it's valuable to have it from the start and it also included some bug fixes.

@youngar @rwy7 Could you take another look at this?

Comment on lines +296 to +353
template <typename ValueTy, typename... Args>
void internalizeResult(Value val, Args &&...args) {
// TODO: this isn't the most efficient way to internalize
auto ptr = std::make_unique<ValueTy>(std::forward<Args>(args)...);
auto *e = ptr.get();
auto [iter, _] = interned.insert({e, std::move(ptr)});
state[val] = iter->second.get();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't updated the internalization logic to a more efficient approach yet. My strategy would be to wait until we have a big input example that we can use the profile this part to find the most efficient implementation. But let me know if you would like to see an improvement already in this PR.

// inserting an object of a derived class of ElaboratorValue.
// The custom MapInfo makes sure that we do a value comparison instead of
// comparing the pointers.
DenseMap<ElaboratorValue *, std::unique_ptr<ElaboratorValue>, InternMapInfo>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you pointed out yesterday, this is not particularly elegant. The reason I did it that way is because unique pointers don't seem to work as set or map keys because the copy constructor is deleted. Maybe I'm just missing something to make this work?
Alternatively, we could just use raw pointers that we deallocate manually in the destructor, but that's not very elegant either.
Any concrete suggestions on how to improve this?

@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch 2 times, most recently from 9f88cb6 to f9c513a Compare December 3, 2024 20:21
Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% familiar with the details of the RTG dialect, but the implementation makes sense to me 👍

lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
lib/Dialect/RTG/Transforms/ElaborationPass.cpp Outdated Show resolved Hide resolved
test/Dialect/RTG/Transform/elaboration.mlir Outdated Show resolved Hide resolved
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch from f9c513a to 505d2a0 Compare December 5, 2024 12:57
@maerhart maerhart force-pushed the maerhart-rtg-elaboration-pass branch from 505d2a0 to 999992a Compare December 6, 2024 15:04
@maerhart maerhart merged commit e6e5dca into main Dec 9, 2024
4 checks passed
@maerhart maerhart deleted the maerhart-rtg-elaboration-pass branch December 9, 2024 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RTG Involving the `rtg` dialect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants